<template>
  <a-spin :spinning="loading">
    <a-form style="max-width: 1000px; margin: 40px auto 0;">
      <a-form-item label="标题" :labelCol="{ span: 5 }" :wrapperCol="{ span: 19 }" :required="true">
        <a-input placeholder="发券标题" style="width:300px;" v-model.trim="model.title" />
      </a-form-item>
      <a-form-item label="优惠券" :labelCol="{ span: 5 }" :wrapperCol="{ span: 9 }" :required="true">
        <a-textarea v-model="model.couponNames" :rows="5" readOnly />
        <a-button type="primary" icon="search" @click="handleSelectCoupon">选择</a-button>
      </a-form-item>
      <a-form-item label="每人发券数量" :labelCol="{ span: 5 }" :wrapperCol="{ span: 19 }" :required="true">
        <a-input-number :min="1" :max="5" placeholder="最大支持5" style="width:300px;" v-model="model.distNum" />
      </a-form-item>
      <a-form-item :wrapperCol="{ span: 24, offset: 2 }">
        会员信息
        <a-table ref="table" size="small" bordered rowKey="id" :columns="columns" :dataSource="dataSource"
          :pagination="ipagination" :loading="loading" class="j-table-force-nowrap" @change="handleTableChange" />
      </a-form-item>
      <a-form-item :wrapperCol="{ span: 24, offset: 12 }">
        <a-button type="primary" @click="nextStep" icon="save">提交</a-button>
      </a-form-item>
    </a-form>

    <SelectCouponList ref="selectCouponForm" @ok="selectOk"></SelectCouponList>
  </a-spin>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import SelectCouponList from './SelectCouponList'
import {filterObj} from '@/utils/util'


export default {
  name: "Step1",
  mixins: [JeecgListMixin],
  components: {
    SelectCouponList
  },
  //接受父组件向子组件传递的数据
  props: ['params'],
  data() {
    return {
      loading: false,
      disableMixinCreated: true,
      model: {
      },
      columns: [
        {
          title: '所属机构',
          align: 'center',
          dataIndex: 'orgName',
        },
        {
          title: '注册门店',
          align: 'center',
          dataIndex: 'shopName',
        },
        {
          title: '手机号',
          align: 'center',
          dataIndex: 'mobile',
        },
        {
          title: '注册时间',
          align: 'center',
          dataIndex: 'regTime',
        },
        {
          title: '注册渠道',
          align: 'center',
          dataIndex: 'regSource',
        },
        {
          title: '性别',
          align: 'center',
          dataIndex: 'sex',
        },
        {
          title: '当前积分',
          align: 'center',
          dataIndex: 'pointsBalance',
        },
        {
          title: '最近到店',
          align: 'center',
          dataIndex: 'recentConsumptionTime',
        },
      ],
      url: {
        list: '/member/posShopMember/list',
      },
    }
  },
  watch: {
    params: {
      deep: true,
      handler(val) {
        console.log('---step1 watch params---', this.params)
        this.model = {}
        this.loadMember()
      }
    }
  },
  created() {
    console.log('---step1 created---', this.params)
    this.model = {}
    this.loadMember()
  },
  methods: {
    handleSelectCoupon() {
      this.$refs.selectCouponForm.search()
      this.$refs.selectCouponForm.title = '选择优惠券'
      this.$refs.selectCouponForm.visible = true
    },
    selectOk(result) {
      console.log('result', result)
      if (result && result.length > 0) {
        console.log('callback set value')
        let couponNames = [], couponIds = []
        for (var r of result) {
          couponNames.push(r.actNm)
          couponIds.push(r.couponId)
        }
        this.$set(this.model, 'couponNames', couponNames.join('\r\n'))
        this.$set(this.model, 'couponIds', couponIds.join(','))
      }
    },
    loadMember() {
      if (this.params.ty === 'M') {
        this.queryParam = { memberIds: this.params.memberIds }
      } else {
        this.queryParam = this.params.queryParam
      }
      this.dataSource = []
      this.loadData()
    },
    nextStep() {
      let couponIds = this.model.couponIds
      let distNum = this.model.distNum
      let title = this.model.title

      if (title == undefined || title == '') {
        this.$message.warning('请输入发券标题')
        return false
      }

      if (couponIds == undefined || couponIds == '') {
        this.$message.warning('请选择优惠券')
        return false
      }
      console.log('distNum', distNum)
      if (distNum == undefined || distNum == '' || distNum == 0) {
        this.$message.warning('请填写数量')
        return false
      }

      let total = this.ipagination.total
      if (total == 0) {
        this.$message.warning('无会员信息')
        return false
      }

      let r = confirm('确定提交发券吗?')
      if (!r) {
        return false
      }

      let callback = {
        couponIds: couponIds,
        distNum: distNum,
        title:title,
        params: filterObj(this.params)
      }
      this.loading = true
      this.$emit('nextStep', callback)
    }
  }
}
</script>

<style scoped>
</style>